<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 用户管理
 */
class Sort extends Auth_Controller {
	public function __construct() {
        parent::__construct();
        $this->load->model('sort_mdl');
    }

	public function index(){
		if($_SESSION['group'] == 3){
			show_error('没有权限');
		}
		$url = '';
		$this->db->start_cache();
		$key = $this->input->get('keywords');
		if(!empty($key)){
			$this->db->like('title',$key);
			$url .= 'keywords='.$key.'&';
		}
		$this->db->stop_cache();

		if(!empty($url)){
            $url = '?'.substr($url, 0 , -1);
        }

		$this->load->library('pagination');

        $config['base_url'] = site_url('sort/index').$url;
        $config['total_rows'] = $this->sort_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('sort desc,id desc');
        $list = $this->sort_mdl->select();

        $this->db->flush_cache();

        $data['list'] = $list;
		$this->_view('index',$data);
	}

	/**
	 * 新增分类
	 */
	public function create(){
		if($_SESSION['group'] == 3){
			show_error('没有权限');
		}

		if($this->input->is_ajax_request()){
			$post = $this->input->post();

			if(empty($post['title'])){
				die('请填写分类名称');
			}

			//判断分类名称是否存在
			$this->db->where('title',$post['title']);
			$r = $this->sort_mdl->get_count();

			if($r > 0){
				die('该分类名称已经存在！不能添加。');
			}

			$data = array(
				'title' => $post['title'],
				'pid' => 0,
				'sort' => 0,
				'described' => $post['content']
			);

			$res = $this->sort_mdl->create($data);

			echo $res > 0 ? 1 : '参数错误！';
		}else{
			$this->_view('create');
		}

	}

	/**
	 * 修改分类
	 */
	public function update(){
		if($_SESSION['group'] == 3){
			show_error('没有权限');
		}

		if($this->input->is_ajax_request()){
			$post = $this->input->post();

			if(empty($post['title'])){
				die('请填写分类名称');
			}

			if(empty($post['sid'])){
				die('参数错误！');
			}

			//判断分类名称是否存在
			$this->db->where('title',$post['title']);
			$this->db->where('id !=',$post['sid']);
			$r = $this->sort_mdl->get_count();

			if($r > 0){
				die('该分类名称已经存在！不能更改。');
			}

			$data = array(
				'title' => $post['title'],
				'described' => $post['content']
			);
            $this->db->where('id',intval($post['sid']));
			$res = $this->sort_mdl->update($data);

			echo $res > 0 ? 1 : '参数错误！';
		}else{
			$sid = $this->input->get('sid');
			$this->db->where('id',$sid);
			$res = $this->sort_mdl->get_one();
			if(empty($res)){
				show_error('参数错误！');
			}
			$this->_view('update',array('res'=>$res));
		}
	}

	/**
	 * 分类排序
	 */
	public function set_sort(){
		if(!$this->input->is_ajax_request()){
			die('请求方法错误！');
		}

		$sid = $this->input->post('sid');
		$val = $this->input->post('sort');

		$this->db->set('sort',$val);
		$this->db->where('id',$sid);
		$res = $this->sort_mdl->update();
		//echo $this->db->last_query();

		echo $res === TRUE ? 1 : '未知错误';
	}

	/**
	 * 分类删除
	 */
	public function delete(){
		if(!$this->input->is_ajax_request()){
			show_error('请求方法错误！');
		}
		$sid = $this->input->post('sid');

		$this->load->model('project_mdl');
		$this->load->model('article_mdl');

		$this->db->where('sort_id',$sid);
		$this->db->set('sort_id',0);
		$this->project_mdl->update();

		$this->db->where('sort_id',$sid);
		$this->db->set('sort_id',0);
		$this->article_mdl->update();

		$this->db->where('id',$sid);
		$res = $this->sort_mdl->delete();
		echo $res === TRUE ? '1' : '发生未知错误！请稍后再试';
	}

	/**
	 * 分类列表
	 */
	public function s_list(){
		if(!$_SESSION['mobile']){
			die('非手机用户禁止访问');
		}
		$this->db->select('id,title');
		$this->db->order_by('sort desc,id desc');
		$list = $this->sort_mdl->select();
		$this->_view('list.php',array('list'=>$list));
	}
}